/**
 *
 * Created by Song,YeWen, 10/9/2011 11:47:42 AM
 *
 */

function replaceSlash(_arg1) {
	while (_arg1.indexOf("\\") != -1) {
		_arg1 = _arg1.replace("\\", "/");
	};
	return (_arg1);
}
function newUrlCdn(_arg1) {
	var _local2 = replaceSlash(_arg1);
	var _local3 = Math.floor((parseInt(_local2.charAt((_local2.lastIndexOf("/") + 1)), 16) / 2));
	var _local4 = _local2.split("/")[3];
	var _local5 = _local4.substr(0, 5);
	var _local6 = parseInt(_local4.substr(5, 2));
	if ((((_local5 == "video")) && ((_local6 <= 5)))){
		_local2 = _local2.replace("video.ifeng", (("video0" + _local3) + ".ifeng"));
	} else {
		if ((((_local5 == "video")) && ((_local6 > 5)))){
			_local2 = _local2.replace("video.ifeng", (("video1" + _local3) + ".ifeng"));
		};
	};
	return (_local2);
}

/* http://v.ifeng.com/news/society/201112/72acd94c-5341-4396-8a2a-ab0a83aa637b.shtml */
/* http://v.ifeng.com/v/jieyujie/index.shtml#00c32661-dfe3-4b0b-b3db-cfdb3aa2673a */
function  validateVideoUrl(ori_url) 
{
    var guid;
	var html = download_page(ori_url);
	var matches = html.match(/"id":"(.+?)"/);
	if (matches)
	    guid = matches[1];
	else {
	    var splash_pos = ori_url.lastIndexOf('/');
	    if (splash_pos != -1) {
	        guid = ori_url.substring(splash_pos+1);
	        var sharp_pos = guid.lastIndexOf('#');
	        if (sharp_pos != -1)
	            guid = guid.substring(sharp_pos+1);
	    }
	    // Not Found
	    if(guid.length < 36)
	  	    return 'NotFoundVideo';
	}	
	var videoXmlUrl = "http://v.ifeng.com/video_info_new/" + guid.charAt(34) + "/" + guid.charAt(34) + guid.charAt(35) + "/" + guid + ".xml";
	var xml_str = download_page(videoXmlUrl);
	var flv_path = parse_xml_attribute(xml_str, 'item', 'VideoPlayUrl');
	var flv_str = newUrlCdn(flv_path);
	var duration = parse_xml_attribute(xml_str, 'item', 'Duration');
	
	//matches = html.match(/<title>(.*?)<\/title>/);
	matches = html.match(/<title>([\s\S]*?)<\/title>/);
    if(matches){
 	   return 'success_title:' + encodeURIComponent(matches[1])
 	          + '\n video_urls:' + encodeURIComponent(flv_str)
 	           + '\n seconds:' + Math.round(parseInt(duration));
    }
    return 'error:empty_path';
}

function js_get_media_url(ori_url) 
{
    var guid;
	var html = download_page(ori_url);
	var matches = html.match(/"id":"(.+?)"/);
	if (matches)
	    guid = matches[1];
	else {
	    var splash_pos = ori_url.lastIndexOf('/');
	    if (splash_pos != -1) {
	        guid = ori_url.substring(splash_pos+1);
	        var sharp_pos = guid.lastIndexOf('#');
	        if (sharp_pos != -1)
	            guid = guid.substring(sharp_pos+1);
	    }
	    // Not Found
	    if(guid.length < 36)
	  	    return 'NotFoundVideo';
	}

    var videoXmlUrl = "http://v.ifeng.com/video_info_new/" + guid.charAt(34) + "/" + guid.charAt(34) + guid.charAt(35) + "/" + guid + ".xml";
	var xml_str = download_page(videoXmlUrl);
	var flv_path = parse_xml_attribute(xml_str, 'item', 'VideoPlayUrl');
	var flv_str = newUrlCdn(flv_path);
	
	return flv_str;
}
